package com.example.test;

import com.example.common.DefaultWatcher;
import org.apache.zookeeper.*;
import org.apache.zookeeper.ZooKeeper.States;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

public class ConnectZK {

    final CountDownLatch cd = new CountDownLatch(1);

    @Test
    public void conn() {

        try {
            ZooKeeper zk = new ZooKeeper("192.168.1.101:2181,192.168.1.102:2181,192.168.1.103:2181,192.168.1.104:2181",
                    3000, new DefaultWatcher(cd));

            cd.await();

            System.out.println("zk state ::: " + zk.getState());

            Stat stat = new Stat();

            byte[] data = zk.getData("/abc", new Watcher() {
                @Override
                public void process(WatchedEvent event) {

                    System.out.println("getData event :: " + event.toString());
                    System.out.println("getData event state :: " + event.getState());
                    System.out.println("getData event type :: " + event.getType());

                    try {
                        zk.getData("/abc", this, stat);
                    } catch (KeeperException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, stat);


            System.out.println("getData :: " + new String(data));
            System.out.println("getData stat : " + stat);


            Stat stat1 = zk.setData("/abc", "cccc".getBytes(), stat.getVersion());
            System.out.println("setdata stat :: " + stat1.toString());

            Stat stat2 = zk.setData("/abc", "eeeee".getBytes(), stat1.getVersion());
            System.out.println("setdata stat :: " + stat2.toString());

            String s = zk.create("/bbb", "aaaaaaa".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);

            byte[] data1 = zk.getData("/bbb", event -> System.out.println(event.toString()), stat);

            System.out.println("bbb inode data :: "  + new String(data1));

            Stat stat3 = zk.setData("/bbb", "bbbbb".getBytes(), 0);
            System.out.println("bbb setData stat ::: " + stat3.toString());


            System.out.println("-------cb start------");
            zk.getData("/bbb", event -> System.out.println(event.toString()), new AsyncCallback.DataCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                    System.out.println("bbb data cb rc == " + rc);
                    System.out.println("bbb data cb path == " + path);
                    System.out.println("bbb data cb ctx == " + ctx);
                    System.out.println("bbb data cb data == " + new String(data));
                    System.out.println("bbb data cb stat == " + stat);
                }
            }, "bbbbbb");
            System.out.println("-------cb end------");


            Thread.sleep(222222);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        }


    }

}
